Object and Type
Object : FCGeneric
Type : Method
Prototype
Public Sub AddNew()
Description
This method allows you to add a new record to a generic object. The object can later be added to the database with either the Update or UpdateAll methods.
When you call AddNew, a new record is allocated at the end of the list of records in this generic object. The new record is set to the current record, and all fields are initialized to their default values (NULL for all relations).
You can then set individual fields via the Fields collection and relate the record to other records using one of the relate methods. FCFL takes care of generating the objid for you – that is not a field you need to set.
If you do not supply an objid, FCFL will generate a proper objid for the newly-created record. This is how the method is used most of the time. In rare cases, however, a programmer may need to use a specific objid for the newly-created record. If you specify an objid for the record (after the AddNew but before the Update/UpdateAll), FCFL will not generate a new objid for the record, but will use the one you supply. You must be very careful that the supplied objid is unique.
Note: If you have previously queried the database for this generic object, and did not query for all fields, the AddNew method will fail with an error. This is because it attempts to initialize all data fields to their proper defaults, and you previously specified that only certain fields should be in this generic object. You can use the RequeryForInserts method to rectify this, or simply create a new Generic object for the insert.
Error Codes
Value Meaning
12009 Could not add record because not all fields were selected in a previous query
12031 This operation is not valid for a generic that is marked distinct
Example
The following example creates a new record for the contact_role table. It also relates it to two objects, and adds the record into the database.
JavaScript:
The code in this example is written in JavaScript for inclusion in ASP pages.
// Get one specific contact
var boContact = FCSession.CreateGeneric();
boContact.SimpleQuery("contact");
boContact.AppendFilter("objid", "=", "268435457");
// Get one specific site
// Query both tables via the bulk
var boSite = FCSession.CreateGeneric();
boSite.SimpleQuery("site");
boSite.AppendFilter("objid", "=", "268435457");
boContact.Bulk.Query();
// Now create a new contact role
// Set it as a secondary role, and relate it to contact and site
// Then commit the change!!
var boCRole = FCSession.CreateGeneric();
boCRole.SimpleQuery("contact_role");
boCRole.AddNew();
boCRole("primary_site") = 2;
boCRole("role_name") = "Default";
boCRole.RelateRecords(boContact, "contact_role2contact");
boCRole.RelateRecords(boSite, "contact_role2site");
boCRole.Update();
Visual Basic:
The code in this example is written in Visual Basic. In this example, the objid is generated by the programmer, not FCFL.
Dim boSite As FCGeneric
Dim boContact As FCGeneric
Dim boCRole As FCGeneric
Set boContact = fc_session.CreateGeneric
boContact.SimpleQuery "contact"
boContact.AppendFilter "objid", "=", 268435457
Set boSite = fc_session.CreateGeneric
boSite.SimpleQuery "site"
boSite.AppendFilter "objid", "=", 268435457
boContact.Bulk.Query
Set boCRole = fc_session.CreateGeneric
boCRole.SimpleQuery "contact_role"
boCRole.AddNew
boCRole = fc_session.GetObjid("contact_role")
boCRole("primary_site") = 2
boCRole("role_name") = "Default"
boCRole.RelateRecords boContact, "contact_role2contact"
boCRole.RelateRecords boSite, "contact_role2site"
boCRole.Update